package com.yixun.qfbao.hander;

import com.yixun.qfbao.api.service.AreaService;
import com.yixun.qfbao.apidto.GetCityReqDto;
import com.yixun.qfbao.apidto.GetCityResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.GetCityReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 获取城市列表
 * @author guifang.weng
 * @create 2017-11-23 13:48
 **/
@Service
public class GetCityHander extends CommonAbstractHander {
    private static Logger logger= LoggerFactory.getLogger(GetCityHander.class);

    @Autowired
    private AreaService areaService;
    
    @Autowired
	private LoginUtils loginUtils;

    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        BaseResponseDTO<GetCityResDto> res = new BaseResponseDTO<>();
        GetCityReqVo reqVo = (GetCityReqVo)reqDto;
        GetCityReqDto getCityReqDto = reqVo.getParams();
        try {
        	GetCityResDto resDto = new GetCityResDto();
        	//从缓存读取城市列表，读取不到时，使用查询方法，再存入缓存
			Object object = loginUtils.get("city_" + getCityReqDto.getAreaId() + "_" + getCityReqDto.getCompanyUuid());
			if(object != null) {
				resDto = (GetCityResDto) object;
                resDto.setResult(ResultEnum.SUCCESS.getNo());
                res.setResult(ResultEnum.SUCCESS.getNo());
			} else {
				resDto = areaService.getCity(getCityReqDto);
                res.setResult(resDto.getResult());
				loginUtils.put("city_" + getCityReqDto.getAreaId() + "_" + getCityReqDto.getCompanyUuid(), resDto);
			}

            res.setResData(resDto);
        } catch (Exception e) {
            logger.error("GetCityHander error: {}", e);
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("获取省份列表失败");
        }
        return res;
    }
}
